﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FixedPropertyManagement.Models;

/*Response.Write("<script>alert('请输入注册为系统管理员的注册密码');</script>");*/
namespace FixedPropertyManagement.aspxs
{
    public partial class Register : System.Web.UI.Page
    {
        PropertyMangementEntities3 db = new PropertyMangementEntities3();
        string id;
        string name;
        string pwd;
        string pwd1;
        string tell;
        int departId = 520;
        string syspwd;

        protected void Page_Load(object sender, EventArgs e)
        {   
        }
        //返回登录界面
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("login.aspx");
        }
        //注册
        protected void btnRegired_Click(object sender, EventArgs e)
        {
            massgaeOk();                  
        }

        private void init()
        {
            id = txtNum.Text.Trim();
            name = txtName.Text.Trim();
            pwd = txtPassword.Text.Trim();
            pwd1 = celPassword.Text.Trim();
            tell = txtTell.Text.Trim();
            syspwd = sysPassword.Text.Trim();
            if (id == "")
            {
                txtNum.Focus();
                return;
            }
            if (pwd == "")
            {
                txtPassword.Focus();
                return;
            }
            if (pwd1 == "")
            {
                celPassword.Focus();
                return;
            }
            if (tell.Length != 11 && tell != "")
            {
                txtTell.Focus();
                return;
            }
            if(name == "")
            {
                txtName.Focus();
                return;
            }
        }

        private void massgaeOk()
        {
            init();        
            if(!pwd1.Equals(pwd))
            {
                txtPassword.Focus();
                Response.Write("<script>alert('密码不一致！')</script>");
                return;
            }
            else
            {
                Employee em = new Employee();
                int number;
                try
                {
                    number = Int32.Parse(id);
                }
                catch (Exception ex)
                {
                    Response.Write("<script>alert('请输入阿拉伯数字的账号，" + ex.Message + "')</script>");
                    txtNum.Focus();
                    return;
                }
                if (!syspwd.Equals("888"))
                {
                    sysPassword.Focus();
                    Response.Write("<script>alert('注册管理员密码不正确或不能为空！')</script>");
                    return;
                }
                else
                {
                    var re = db.Employees.Where(r => r.EmpId == number);
                    if (re.Count() == 0)
                    {
                        em.EmpId = number;
                        em.Password = pwd;
                        em.DepartId = departId;
                        em.Name = name;
                        em.IsManger = true;
                        if (!tell.Equals("")) em.Tell = tell;
                        try
                        {
                            db.Employees.Add(em);
                            var de = db.Departments.FirstOrDefault();
                            de.Count = de.Count + 1;
                            db.SaveChanges();
                            Response.Write("<script>alert('注册成功，请登录！')</script>");
                            Response.Write("<script>window.location = 'login.aspx';</script>");

                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                    else
                    {
                        txtNum.Focus();
                        Response.Write("<script>alert('系统中已存在该管理员！')</script>");
                    }

                }
            }
        }
    }
}